﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using NPOI.POIFS.FileSystem;
using YiSha.Enum;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:51
    /// 描 述：商铺管理业务类
    /// </summary>
    public class ShopBLL
    {
        private ShopService shopService = new ShopService();

        #region 获取数据
        public async Task<TData<List<ShopEntity>>> GetList(ShopListParam param)
        {
            TData<List<ShopEntity>> obj = new TData<List<ShopEntity>>();
            obj.Data = await shopService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<ShopEntity>>> GetPageList(ShopListParam param, Pagination pagination)
        {
            TData<List<ShopEntity>> obj = new TData<List<ShopEntity>>();
            obj.Data = await shopService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<ShopEntity>> GetEntity(long id)
        {
            TData<ShopEntity> obj = new TData<ShopEntity>();
            obj.Data = await shopService.GetEntity(id);

            var list = shopService.BaseRepository().IQueryable<ShopBrandRelation>(t => t.shopid == obj.Data.Id);

            obj.Data.brandids = string.Join(',', list.Select(t => t.brandid).ToList());

            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(ShopEntity entity)
        {
            TData<string> obj = new TData<string>();

            //判断当前用户是否已经绑定过别的商品
            var expression = LinqExtensions.True<ShopEntity>();
            expression=expression.And(t => t.user_id == entity.user_id && t.Id != entity.Id);

            int count = shopService.BaseRepository().FindList<ShopEntity>(expression).Result.Count();

            if (count > 0)
            {
                obj.Tag = 0;
                obj.Message = "当前门店管理员已在别的店铺绑定,请重新更换";
                return obj;
            }

            if (entity.shop_type.Contains(((int)ShopStatusEnum.装备超市).ToString()))
            {
                var expressionStore = LinqExtensions.True<ShopEntity>();
                expressionStore = expressionStore.And(t => t.shop_type.Contains(((int)ShopStatusEnum.装备超市).ToString()) && t.Id != entity.Id);

                int countStore = shopService.BaseRepository().FindList<ShopEntity>(expression).Result.Count();
                if (countStore>0)
                {
                    obj.Tag = 0;
                    obj.Message = "超市只能添加一个";
                    return obj;
                }
            }
            

            await shopService.SaveForm(entity);

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await shopService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
